<template>
	<div class="force_amend_wrapper">
		<div class="sw_button_box">
			<template v-if="$isHasAuth(['4d42454a662a4571a9c948f7de153460'])">
				<el-button class="btn" size="small" type="primary" @click="handleAllForceAmend('2')">全部关闭强制</el-button>
				<el-button class="btn" size="small" type="primary" @click="handleAllForceAmend('1')">全部开启强制</el-button>
			</template>
			<el-form size="small" inline>
				<el-form-item>
					<el-input placeholder="请输入用户账号" clearable v-model="queryData.account" style="width: 150px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input placeholder="请输入用户姓名" clearable v-model="queryData.userName" style="width: 150px"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" icon="el-icon-search" @click="queryUserListData">查询</el-button>
				</el-form-item>
			</el-form>
		</div>
		<div class="sw_table_container" v-loading="loading">
			<el-table size="medium" :data="userListData" stripe height="100%">
				<el-table-column label="账号" prop="account"></el-table-column>
				<el-table-column label="姓名" prop="userName"></el-table-column>
				<el-table-column label="所属部门" prop="deptName"></el-table-column>
				<el-table-column label="是否强制">
					<template slot-scope="{ row }">
						<el-tag size="small" v-if="row.isForce === '1'" type="success">是</el-tag>
						<el-tag size="small" v-if="row.isForce === '2'" type="danger">否</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" width="90" v-if="$isHasAuth(['8d9bb9c3bfdb4064826b7e84c1830032'])">
					<template slot-scope="{ row }">
						<el-button size="medium" type="text" @click="handleForceOperate(row)">{{ row.isForce === '1' ? '关闭强制' : '开启强制' }}</el-button>
					</template>
				</el-table-column>
			</el-table>
		</div>
		<template v-if="dataCount > 0">
			<sw-pagination :total="dataCount" :page.sync="queryData.page" :limit.sync="queryData.limit" @change="queryUserListData"></sw-pagination>
		</template>
	</div>
</template>

<script>
import { getUserList, forceUpdateAllUserPassword, forceUpdateUserPassword } from '@/api/system/tbSysUser';
export default {
	data() {
		return {
			loading: false,
			queryData: { page: 1, limit: 15 },
			dataCount: 0,
			userListData: []
		};
	},
	mounted() {
		this.queryUserListData();
	},
	methods: {
		//获取用户列表
		async getUserListData() {
			let result = await getUserList(this.queryData).then((res) => res.data);
			this.loading = false;
			if (result.code === '0') {
				this.dataCount = parseInt(result.count);
				this.userListData = !result.data ? [] : result.data;
			}
		},

		//查询用户列表
		queryUserListData() {
			this.loading = true;
			this.getUserListData();
		},

		//开启/关闭强制修改
		async handleForceOperate(row) {
			let isForce = row.isForce === '1';
			let params = { id: row.id, isForce: isForce ? '2' : '1' };
			let result = await forceUpdateUserPassword(params).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `${isForce ? '关闭' : '开启'}强制修改成功`);
				row.isForce = isForce ? '2' : '1';
			}
		},

		//全部开启/关闭强制修改
		async handleAllForceAmend(value) {
			let isForce = value === '1';
			let result = await forceUpdateAllUserPassword(value).then((res) => res.data);
			if (result.code === '0') {
				this.$showCustomMessage('success', `全部用户${isForce ? '关闭' : '开启'}强制修改成功`);
				this.queryUserListData();
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.force_amend_wrapper {
	display: flex;
	flex-direction: column;
	overflow: hidden;
	padding: 16px;
	flex: 1;
	&:deep(.sw_button_box) {
		margin-bottom: 16px;
		.el-form-item {
			margin-bottom: 0;
		}
		.el-button.btn {
			float: right;
			margin-left: 10px;
		}
	}
}
</style>
